package pers.ervinse.shoppingmall.gpt.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import androidx.fragment.app.FragmentActivity;

import pers.ervinse.shoppingmall.R;
import pers.ervinse.shoppingmall.gpt.fragment.ChatFragment;

public class ChatAdapter extends BaseAdapter {

    private FragmentActivity fragmentActivity;

    public ChatAdapter(FragmentActivity fragmentActivity) {
        this.fragmentActivity = fragmentActivity;
    }

    @Override
    public int getCount() {
        return 1;  // 只有一个聊天框
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(fragmentActivity).inflate(R.layout.page_community, parent, false);
        }

        // 获取 FrameLayout 容器，用来放置 ChatFragment
        FrameLayout frameLayout = convertView.findViewById(R.id.chat_container);

        // 创建 ChatFragment 实例
        ChatFragment chatFragment = new ChatFragment();

        // 使用 FragmentTransaction 将 ChatFragment 添加到 FrameLayout
        if (!chatFragment.isAdded()) {
            fragmentActivity.getSupportFragmentManager().beginTransaction()
                    .replace(R.id.chat_container, chatFragment)
                    .commit();
        }

        return convertView;
    }
}
